/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project.DataLayer;

import DataModel.*;
import Utilities.*;
import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Rainiero
 */
public class PaymentDataManager {

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    private static PaymentDataManager _instance = new PaymentDataManager();

    private PaymentDataManager()
    {
    }

    public static PaymentDataManager GetInstance()
    {
        return _instance;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Public Methods">
    public PaymentBase[] GetProjectPayments(int projectId)
    {
        DataAccesor accesor = DataAccesor.GetInstance();
        String spName = "ObtenerPagos";
        SQLParameter[] parameters = new SQLParameter[]
        {
            new SQLParameter("idProyecto", projectId)
        };
        try
        {
            ResultSet reader = accesor.ExecuteInputSP(spName, parameters);
            LinkedList<PaymentBase> result = new LinkedList<PaymentBase>();
            while(reader.next())
                result.add(buildPayment(reader));
            reader.close();
            PaymentBase[] resultArray = new PaymentBase[result.size()];
            return result.toArray(resultArray);
        }
        catch(Exception ex)
        {
            return null;
        }
    }

    public Payment GetPayment(int paymentId)
    {
        DataAccesor accesor = DataAccesor.GetInstance();
        String spName = "ObtenerPago";
        SQLParameter[] parameters = new SQLParameter[]
        {
            new SQLParameter("idDesembolso", paymentId)
        };
        try
        {
            ResultSet reader = accesor.ExecuteInputSP(spName, parameters);
            PaymentBase base = null;
            Payment result = null;
            if(reader.next())
                base = buildPayment(reader);
            reader.close();
            if(base != null)
            {
                spName = "ObtenerProductos";
                parameters = new SQLParameter[]
                {
                    new SQLParameter("idDesembolso", paymentId)
                };
                reader = accesor.ExecuteInputSP(spName, parameters);
                LinkedList<Product> productsList = new LinkedList<Product>();
                while(reader.next())
                {
                    String name = reader.getString("NombreProductoProyecto");
                    String description = reader.getString("DescripcionProductoProyecto");
                    Product product = new Product(name, description);
                    productsList.add(product);
                }
                reader.close();
                Product[] productsArray = new Product[productsList.size()];
                productsArray = productsList.toArray(productsArray);
                if(base.getStatus() == Enums.PaymentStatus.Pending)
                    result = new Payment(base.getId(), base.getEstimatedDate(), base.getEstimatedAmount(), -1, productsArray);
                else
                    result = new Payment(base.getId(), base.getEstimatedDate(), base.getEstimatedAmount(), -1, productsArray, base.getRealDate(), base.getRealAmount());
            }
            return result;
        }
        catch(Exception ex)
        {
            return null;
        }
    }

    public void RegisterPayment(int paymentId, Date date, double amount)
    {
        DataAccesor accesor = DataAccesor.GetInstance();
        String spName = "RegistrarDesembolso";
        SQLParameter[] parameters =
            {new SQLParameter("idDesembolso", paymentId),
            new SQLParameter("fecha", date),
            new SQLParameter("monto", amount)};
        try
        {
            accesor.ExecuteOutputSP(spName, parameters);
        }
        catch(Exception ex)
        {
        }
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Private Methods">
    private PaymentBase buildPayment(ResultSet reader) throws SQLException
    {
        int id = reader.getInt("IdDesembolsoProyecto");
        Date estimatedDate = reader.getDate("FechaEntrega");
        double estimatedAmount = reader.getDouble("MontoDolares");
        Enums.PaymentStatus status = EnumConverter.GetPaymentStatusFromString(reader.getString("Estado"));
        if(status == Enums.PaymentStatus.Pending)
            return new PaymentBase(id, estimatedDate, estimatedAmount);
        else
        {
            Date realDate = reader.getDate("FechaEntregaReal");
            double realAmount = reader.getDouble("MontoReal");
            return new PaymentBase(id, estimatedDate, estimatedAmount, realDate, realAmount);
        }
    }
    // </editor-fold>

}
